package tech.aistar.controller.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.entity.*;
import tech.aistar.service.center.IAddressService;
import tech.aistar.service.center.IRealNameService;
import tech.aistar.service.center.IUserInfoService;
import tech.aistar.service.comment.IAnswerService;
import tech.aistar.service.comment.ICommentService;
import tech.aistar.service.comment.ICommentVoService;
import tech.aistar.service.demand.IPictureService;
import tech.aistar.service.services.IServicesService;
import tech.aistar.util.DateUtileDemo;
import tech.aistar.vo.CommentVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/10/10
 * @Version: 1.0
 */
//@SuppressWarnings("all")
@Controller
@RequestMapping("/services")
@SuppressWarnings("all")
public class ServicesDetailController {
    @Autowired
    private ICommentService commentService;

    @Autowired
    private IAnswerService answerService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IServicesService servicesService;

    @Autowired
    private ICommentVoService commentVoService;

    @Autowired
    private IUserInfoService iUserInfoService;
    @Autowired
    private IAddressService iAddressService;
    @Autowired
    private IRealNameService iRealNameService;
    @Autowired
    private IPictureService iPictureService;

    @GetMapping("/serviceDetailView")
    public String service(Model model,HttpSession session){
        //评论
        List<CommentVo> commentVoList = commentVoService.comvoFindAll();
        model.addAttribute("commentVoList",commentVoList);


        Integer uid = (Integer) session.getAttribute("userinfo_id");
        Picture picture2 = iPictureService.findPathById(uid);
        session.setAttribute("picture2",picture2);
        //时间格式的转换


        Iterator<CommentVo> iter = commentVoList.iterator();
        while(iter.hasNext()){
            CommentVo commentVo = iter.next();
            Date time = commentVo.getTime();

            String comtime = DateUtileDemo.format("yyyy-MM-dd HH:mm:ss",time);
            session.setAttribute("comtime",comtime);
        }



        Integer userinfo4 = (Integer) session.getAttribute("userinfoId");
        List<Answer> answer1 = answerService.userId(userinfo4);
        model.addAttribute("answer1",answer1);

        return "services/serviceDetail";

    }
    //添加评论
    @RequestMapping("/addanswer")

    public String answer(HttpServletRequest request, HttpSession session) {

        Answer answer = new Answer();

        Integer userinfo = (Integer) session.getAttribute("userinfoId");
        String names = userInfoService.findById(userinfo).getName();
        //评论的内容
        String content = request.getParameter("cm");
//        String aname = request.getParameter("names");
//        String hname = request.getParameter("sum");
        String atimes = request.getParameter("atimes");

        answer.setStatus(0);
        answer.setTime(new Date());
        answer.setContent(content);
        answer.setUserinfoId(userinfo);
        answer.setName(names);
        answerService.anSave(answer);

        return "redirect:/services/serviceDetailView";

    }



    //服务详情
    @ResponseBody
    @RequestMapping("/serviceDetail")
    public Result serviceDetail(Integer id ,HttpSession session ,Model model){
        Services services = servicesService.findById(Integer.valueOf(id));
        UserInfo userinfo = userInfoService.ById(services.getUserinfoId());
        Picture picture = iPictureService.findPathById(userinfo.getId());

        session.setAttribute("services",services);
        session.setAttribute("userinfo",userinfo);
        session.setAttribute("picture",picture);

        return new Result(CodeStatus.OK,true,"success",services);
    }

    //通过服务服务详情页查看服务人员主页
    @GetMapping("/serHomePage")
    public String center(Model model,HttpServletRequest req , Integer id, HttpSession session){

        Integer uid = Integer.valueOf(req.getParameter("serId"));
        UserInfo userInfo = iUserInfoService.ById(uid);
        List<Address> address = iAddressService.findAllById(uid);
        RealName realName = iRealNameService.findByUserId(uid);
        Picture picture = iPictureService.findPathById(userInfo.getId());

        model.addAttribute("userinfo",userInfo);
        model.addAttribute("address",address);
        model.addAttribute("realName",realName);
        model.addAttribute("picture",picture);


        //评论
        List<Comment> commentList = commentService.comFindser(2);
        //获取评论人的id
        List<UserInfo> userInfos = new ArrayList<>();
        for (int i = 0; i < commentList.size(); i++) {
            System.out.println(commentList.get(i).getUserinfoId());
            //获取userinfoId 评论的姓名
            UserInfo userInfo1 = userInfoService.findById(commentList.get(i).getUserinfoId());
            userInfo1.setId(commentList.get(i).getUserinfoId());
            userInfos.add(userInfo1);
        }
        Integer userinfo4 = (Integer) session.getAttribute("userinfoId");
        List<Answer> answer1 = answerService.userId(userinfo4);
        //放入作用域
        model.addAttribute("answer1",answer1);
        model.addAttribute("commentList",commentList);
        model.addAttribute("userInfos",userInfos);

        return "services/serHomePage";
    }
}




